package cn.johnyu.netdemo02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startServer(View view) {

        new Thread(){
            @Override
            public void run() {
                try {
                    ServerSocket server = new ServerSocket(4440);
                    Socket sk = server.accept();
                    OutputStream out = sk.getOutputStream();
                    InputStream in = sk.getInputStream();
                    //BufferedInputStream oin=new BufferedInputStream(in,256);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String line = reader.readLine();

                    Log.i("john", line);

                    out.write("Hello I am Server!!!".getBytes());


                    out.close();
                    sk.close();
                }
                catch (Exception e){}
            }
        }.start();


    }

    public void sendToServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    Socket sk=new Socket("172.30.222.1",4440);
                    OutputStream out=sk.getOutputStream();
                    out.write("I am Client!\n".getBytes());

                    BufferedReader reader=new BufferedReader(new InputStreamReader(sk.getInputStream()));
                    String line =reader.readLine();
                    Log.i("john",line+"...........................................");

                    reader.close();
                    out.close();
                    sk.close();
                }
                catch (Exception e){}
            }
        }.start();

    }

    public void startUDPServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    byte[] buf=new byte[1024];
                    DatagramSocket sk=new DatagramSocket(7777);
                    DatagramPacket p=new DatagramPacket(buf, 1024);
                    sk.receive(p);
                    byte[] data=p.getData();
                    String s=new String(data,0,p.getLength());
                    Log.i("john","rev:............."+s);

                    InetAddress remoteAddr=p.getAddress();
                    int remotePort=p.getPort();
                    DatagramPacket sp=new DatagramPacket("guai...".getBytes(), 6, remoteAddr, remotePort);
                    sk.send(sp);


                    sk.close();
                }
                catch (Exception e){}
            }
        }.start();

    }

    public void sendToUDPServer(View view) {
        new Thread(){
            @Override
            public void run() {
                try{
                    DatagramSocket sk=new DatagramSocket();
                    String msg="I am John with UDP";
                    InetAddress addr=InetAddress.getByName("172.30.222.1");

                    DatagramPacket p=new DatagramPacket(msg.getBytes(), msg.length(), addr, 7777);
                    sk.send(p);

                    byte[] recBuf=new byte[1024];
                    DatagramPacket recP=new DatagramPacket(recBuf, 1024);
                    sk.receive(recP);

                    String recmsg=new String(recP.getData(),0,recP.getLength());
                    Log.i("john",recmsg+"#########################################################");


                    sk.close();
                }catch (Exception e){}
            }
        }.start();

    }
}
